<?php
class InfoAction extends CommonAction {

    public function details() {
        if(isset($_GET['id'])) {
            C('TOKEN_ON',false);
            $info = D('Info');
            if(!$info->where('id='.$_GET['id'])->getField('id')) $this->error('抱歉，该信息可能已被删除或过期!', 0);
            $nav = M('InfoNav');
            $attr = M('InfoAttr');
            $this->_getSomeInfo($info, $nav);
            $details = $info->field(array('id',
                                          'title',
                                          'attr',
                                          'keep',
                                          'user',
                                          'email',
                                          'lianxi',
                                          'phone',
                                          'qq',
                                          'phone',
                                          'thumbnail',
                                          'content',
                                          'view_count',
                                          'addtime',
                                          'cid',
                                          'addip'))
                            ->where('id='.$_GET['id'])
                            ->find();

            //过期时间处理
            $keep = $details['keep'];
            $oneDay = 3600*24;
            switch($keep) {
                case '永久有效':
                    $details['keep'] = '无限';
                    break;
                case '一年':
                    $oneYear = 3600*24*365;
                    $shut = $oneYear+$details['addtime'];
                    $leave = -floor(time() - $shut);     //向下取整时间
                    $details['keep'] = floor($leave / $oneDay);
                    break;
                case '一月':
                    $oneMonth = 3600*24*30;
                    $shut = $oneMonth+$details['addtime'];
                    $leave = -floor(time() - $shut);
                    $details['keep'] = floor($leave / $oneDay);
                    break;
                case '一周':
                    $oneWeek = 3600*24*7;
                    $shut = $oneWeek+$details['addtime'];
                    $leave = -floor(time() - $shut);
                    $details['keep'] = floor($leave / $oneDay);
                    break;
            }

            //缩略图处理
            $details['thumbnail'] = unserialize($details['thumbnail']);
            foreach($details['thumbnail'] as $key=>$value) {
                if($value == '') unset($details['thumbnail'][$key]);
            }

            //获取原图
            foreach($details['thumbnail'] as $key=>$value) {
                $bigPic = str_replace('s_', '', $value);
                $details['bigsrc'][] = $bigPic;
            }

            //属性处理
            $details['attr'] = explode(';', $details['attr']);
            $tempArr = $details['attr'];          //临时数组
            $details['attr'] = array();         //清空原属性
            foreach($tempArr as $key=>$value) {
                $temp = explode('/', $value);
                $k = $attr->where("nickname='{$temp[0]}'")->getField('name');
                $v = $temp[1];
                $details['attr'][$k] = $v;
            }

            //获取该信息所属分类的其他分类导航
            $parentId = $nav->where('id='.$details['cid'])->getField('pid');
            $otherNav = $nav->field(array('id', 'name'))->where('pid='.$parentId)->select();

            //随机该类获得可能感兴趣信息
            $slidId = '';       //所有同辈的导航id
            foreach($otherNav as $key=>$value) {
                $slidId .= $value['id'].',';
            }
            $slidId = substr($slidId, 0, -1);
            $map['cid'] = array('in', $slidId);
            $mayLike = $info->field(array('id', 'title', 'thumbnail'))
                            ->where($map)
                            ->order('RAND()')
                            ->limit(4)
                            ->select();
            foreach($mayLike as $key=>&$value) {
                $temp = unserialize($value['thumbnail']);
                if(empty($temp[0]) && empty($temp[1]) &&empty($temp[2])) $value['thumbnail'] = 0;
            }

            //防止不间断刷新浏览器次数
            $cookieName = 'view_limit'.$_GET['id'];
            if(!isset($_COOKIE[$cookieName])) {
                //增加浏览次数
                $info->where('id='.$_GET['id'])->setInc('view_count', 1);
                setcookie('view_limit'.$_GET['id'], 1, time()+60*10, '/');
            }

            $this->assign(array(
                'details'=>$details,
                'othernav'=>$otherNav,
                'maylike'=>$mayLike
            ));

            $this->display();
        }
    }

    public function update() {
        $id = $this->_get('id');
        if(isset($id)) {
            $info = D('Info');
            $nav = M('InfoNav');
            $attr = M('InfoAttr');
            $this->_getSomeInfo($info, $nav);

            //判断是否当前用户操作
            $postUser = $info->where('id='.$id)->getField('user');
            if($postUser != $_COOKIE['user']) $this->error('本信息不属于您发布，您无权修改!');

            //列举信息属性
            $cnav = $info->where('id='.$id)->getField('cid');
            $pnav = $nav->where('id='.$cnav)->getField('pid');
            $attrsId = $nav->where('id='.$pnav)->getField('attr');
            $map['id'] = array('in', $attrsId);
            $myAttr = $attr->where($map)->select();
            foreach($myAttr as $key=>&$value) {
                $value['details'] = explode(',', $value['details']);
            }

            //当前信息的所有属性
            $currentAttr = $info->where('id='.$id)->getField('attr');
            $currentAttrArr = array();
            $temp = explode(';', $currentAttr);
            foreach($temp as $key=>&$value) {
                $t = explode('/', $value);
                $k = $t[0];
                $v = $t[1];
                //复选框特殊处理，按照逗号分成数组
                $attrType = $attr->where("nickname='$k'")->getField('type');
                if($attrType == 2)  $v = explode(',', $v);
                $currentAttrArr[$k] = $v;

            }

            //当前信息的具体
            $details = $info->field(array('id', 'title', 'keep', 'thumbnail', 'content', 'lianxi', 'phone', 'qq', 'email'))
                            ->where('id='.$id)
                            ->find();
            $details['thumbnail'] = unserialize($details['thumbnail']);
            //修改
            if(isset($_POST['send'])) {
                $info = D('Info');
                if($info->create()) {
                    if($info->save()) $this->success('信息修改成功', __URL__.'/details/id/'.$_GET['id'].'.html');
                } else {
                    $this->error($info->getError());
                }
            }

            $this->assign(array(
                'attr'=>$myAttr,
                'cattr'=>$currentAttrArr,
                'details'=>$details
            ));

            $this->display();
        }
    }

    //删除
    public function delete() {
        $id = $this->_get('id');
        if($id) {
            $info = M('Info');
            //判断是否当前用户操作
            $postUser = $info->where('id='.$id)->getField('user');
            if($postUser != $_COOKIE['user']) $this->error('本信息不属于您发布，您无权删除!');

            $info->where('id='.$id)->delete() ? $this->success('删除成功', __ROOT__.'/member.php/Index/myInfo') : $this->error('系统故障，删除失败!');
        }
    }

    //ajax发短信
    public function ajaxPostMessage() {
        //限制频繁发送
        if(cookie('limit')) {
            echo 'so fast';
            exit;
        }

        $message = M('Message');
        $mto = $this->_post('mto');
        $title = $this->_post('title');
        $content = $this->_post('content');
        $mfrom = $this->_post('mfrom');
        $date = date('Y-m-d H:i:s');
        $data = array(
            'mto'=>$mto,
            'mfrom'=>$mfrom,
            'title'=>$title,
            'content'=>$content,
            'is_read'=>0,
            'date'=>$date
        );

        if($message->add($data)) {
            setcookie('limit', 1, time()+60*3, '/');
            echo 1;
        }
    }

    //ajax置顶
    public function ajaxStick() {
        $user = M('User');
        $info = M('Info');
        $level = $this->_post('level');
        $days = $this->_post('days');
        $pay = $this->_post('pay');
        $id = $this->_post('id');
        if($user->where("name='{$_COOKIE['user']}'")->getField('account') < $pay) exit('余额不足');
        //处理置顶时长
        switch($days) {
            case 1:
                $days = 3600*24;
                break;
            case 2:
                $days = 3600*24*7;
                break;
            case 3:
                $days = 3600*24*30;
                break;
        }
        $temp = $info->field(array('stick', 'stick_days'))->where("user='{$_COOKIE['user']}'")->find();
        if($temp['stick'] == $level && $temp['stick_days'] == $days) exit('重复');
        //扣除用户金币
        $r1 = $user->where("name='{$_COOKIE['user']}'")->setDec('account', $pay);
        //信息置顶
        $data['stick'] = $level;
        $data['stick_days'] = $days;
        $data['stick_time'] = time();
        $r2 = $info->where('id='.$id)->save($data);
        echo $r1 && $r2 ? 1 : 0;
    }

    private function _getSomeInfo(&$info, &$nav) {
        $cid = $info->where('id='.$_GET['id'])->getField('cid');
        $title = $info->where('id='.$_GET['id'])->getField('title');
        $pid = $nav->where('id='.$cid)->getField('pid');
        $childName = $nav->where('id='.$cid)->getField('name');
        $parentName = $nav->where('id='.$pid)->getField('name');
        $this->assign(array(
            'childname'=>$childName,
            'parentname'=>$parentName,
            'title'=>$title,
            'pid'=>$pid,
            'cid'=>$cid,
            'empty'=>'<p style="color:red;">暂无相关图片 :(</p>'
        ));
    }


}

?>